/**
 * Created by wangshaohua on 15/12/27.
 */
moduleMain.controller('LoginController', function (LanguageService, LanguageResource, $scope, $state, $log, $translate, LoadingService, ConstantService, LoginResource, TokenService, DialogService, EnumResource) {
    $scope.loginData = {};
    $scope.langOptions = Enums.LangEnum.options;

    $scope.$on("$viewContentLoaded", function () {
        ConstantService.clear();
    });

    $scope.login = function () {

        LoginResource.login($scope.loginData, function (loginResult) {
            $log.debug(LanguageService.language('登录用户信息'), loginResult.data);
            TokenService.setToken(loginResult.data);
            ConstantService.setItem('user', loginResult.data);
            $scope.initEnum();
            $state.go("sz.homepage");
        });
    };

    $scope.initEnum = function () {
        EnumResource.data({}, {}, function (result) {
            Enums = result.data;
            Enums.loaded = true;
            Enums.LangEnum = {
                CN: 'cn',
                EN: 'en',
                options: {
                    FALSE: {name: LanguageService.language('中文'), value: 'cn'},
                    TRUE: {name: 'English', value: 'en'}
                }
            };

            Enums.BooleanEnum = {
                FALSE: false,
                TRUE: true,
                options: {
                    FALSE: {name: LanguageService.language('否'), value: false},
                    TRUE: {name: LanguageService.language('是'), value: true}
                }
            };

            Enums.BooleanNumberEnum = {
                FALSE: '0',
                TRUE: '1',
                options: {
                    FALSE: {name: LanguageService.language('否'), value: 0},
                    TRUE: {name: LanguageService.language('是'), value: 1}
                }
            };

            ConstantService.setItem('Enums', Enums);


        })
    }

    $scope.switchLang = function (lang) {
        $scope.currentLang = lang;
        $translate.use($scope.currentLang.key);
        window.localStorage.lang = $scope.currentLang.key;
        window.location.reload();
    };

    $scope.changePassword = function () {
        DialogService.open("changePassword");
    };

    var checkBg = function () {
        var loginContainer = document.getElementById("loginContainer");
        var loginBody = document.getElementById("loginBody");
        if (!!$scope.showBgpic) {
            loginContainer.style.background = "url(skin/default/libs/atlanta/img/bg.png) left top repeat #1b1e24";
            loginBody.style.background = "rgba(0, 0, 0, 0.4)";
        } else {
            loginContainer.style.background = "url(skin/default/libs/atlanta/img/bg.png) left top repeat #1b1e24";
            loginBody.style.background = "rgba(255, 255, 255, 0.1)";
        }
    };

    document.onkeydown = function (e) {
        if (e.ctrlKey && e.keyCode == "81") {
            $scope.showBgpic = !$scope.showBgpic;
            checkBg();
        }
    };

    $scope.showBgpic = true;
    checkBg();

});